<?php

namespace catchAdmin\apipatient\controller;

use catcher\exceptions\FailedException;
use think\Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use think\exception\ValidateException;
use catchAdmin\drug\service\MedicationReminderService;
use app\common\model\MedicationReminder as ReminderModel;
use app\common\model\MedicationReminderHistory as HistoryModel;
use catchAdmin\apipatient\validate\MedicationReminderValidate;
use catchAdmin\apipatient\validate\MedicationOperationsValidate;
use app\common\model\Drug as DrugModel;

class MedicationReminder extends CatchController
{
    /**
     * 我的药盒
     * @return \think\response\Json
     */
    public function getMedicineBox(): \think\response\Json
    {
        $patientId = request()->userInfo['patientId'];
        $where[] = ['patientId', '=', $patientId];
        return CatchResponse::paginate((new ReminderModel())->getList($where, 'drug_name,usage'));
    }

    /**
     * 用药提醒
     * @return \think\response\Json
     */
    public function indexReminder(): \think\response\Json
    {
        $patientId = request()->userInfo['patientId'];
        $where[] = ['patientId', '=', $patientId];
        return CatchResponse::paginate((new ReminderModel())->getList($where));
    }

    /**
     * 添加用药提醒
     * @param Request $request
     * @return \think\response\Json
     */
    public function addReminder(Request $request): \think\response\Json
    {
        $req = $request->post();
        try {
            validate(MedicationReminderValidate::class)->check($req);
            $ret = (new MedicationReminderService())->saveMedicationReminder($req);
            return CatchResponse::success($ret);
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 服药操作
     * @param Request $request
     * @return \think\response\Json
     */
    public function MedicationOperations(Request $request): \think\response\Json
    {
        $req = $request->post();
        try {
            validate(MedicationOperationsValidate::class)->check($req);
            $where['operation_time'] = date("Y-m-d");
            $where['medication_id'] = $req['medication_id'];
            $where['take_time_id'] = $req['take_time_id'];
            $is_operation = (new HistoryModel())->where($where)->field('id')->find();
            if ($is_operation['id']) {
                throw new FailedException('今天您已标记过该时段的服药状态');
            }
            $req['operation_time'] = date("Y-m-d");
            $req['deleted_at'] = 0;
            return CatchResponse::success((new HistoryModel())->addData($req));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取药品
     * @param Request $request
     * @return \think\response\Json
     */
    public function getHzdDrug(): \think\response\Json
    {
        $list = (new DrugModel())->field('code,name')->order('created_at', 'desc')->select();
        return CatchResponse::success($list);
    }

    /**
     * 用药提醒单位
     * @return \think\response\Json
     */
    public function getHzdReminderUnit(): \think\response\Json
    {
        $cycle = config('dictionary.medication_cycle');
        $frequency = config('dictionary.reminder_frequency');
        $ret = [
            'cycle' => $cycle,
            'frequency' => $frequency
        ];
        return CatchResponse::success($ret);
    }

//    /**
//     * 用药记录
//     * @param Request $request
//     * @return \think\response\Json
//     */
//    public function getMedicationRecord(Request $request): \think\response\Json
//    {
//        $req = $request->param();
//
//        $where = [];
//        //类型 1 当前用药，2 历史用药
//        if ($req['type'] == 1) {
//            $where[] = ['cycle_unit', '=', '1'];
//        }
//        return CatchResponse::success();
//    }
}